package com.sheng.whyclass.work.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.work.adapter.AllFinishedStudentAdapter;
import com.sheng.whyclass.work.event.FindOneFinishedStudentEvent;

import de.greenrobot.event.EventBus;

public class StudentAllInfoAcitivity extends AppCompatActivity {

    private ListView workList;
    private AllFinishedStudentAdapter adapter;

    private String s_id;

    public static void startStudentAllInfoAcitivity(Context context, String id) {
        Intent intent = new Intent(context, StudentAllInfoAcitivity.class);
        Bundle b = new Bundle();
        b.putString(Constant.KEY_CONTENT_ID, id);

        intent.putExtras(b);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_all_info_acitivity);
        EventBus.getDefault().register(this);
        perseIntent();
        initView();
    }

    private void initView() {

        StringRequest request  = HttpProcessManager.getInstance().findOneStudentInfo(Constant.HOST_FIND_ONE_FINISHED_STUDENT, s_id);
        VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);

        adapter = new AllFinishedStudentAdapter(this);
        workList = (ListView) findViewById(R.id.work_list);
        workList.setAdapter(adapter);

    }

    private void perseIntent() {
        Intent intent = getIntent();
        if (intent == null || intent.getExtras() == null) {
            return;
        }

        Bundle b = intent.getExtras();

        s_id = b.getString(Constant.KEY_CONTENT_ID);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(FindOneFinishedStudentEvent event) {


        int code = event.code;
        if (event.list == null) {
            return;
        }

        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
            adapter.setData(event.list);
        }

    }
}
